<template>
  <div class="workMent">
    <!-- 1: 1-10 2: 11-20 slice((当前页数 - 1)*每页条数, 当前页数*当前条数)-->
    <el-table :data="countData" stripe border style="width: 100%">
      <el-table-column prop="id" label="用户id" align="center">
      </el-table-column>
      <el-table-column prop="userId" label="所属班级" align="center">
      </el-table-column>
      <el-table-column prop="title" label="作业名称" align="center">
      </el-table-column>
      <el-table-column prop="completed" label="完成情况" align="center">
        <template #default="{ row }">
          <el-switch active-color="#13ce66" v-model="row.completed">
          </el-switch>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="Number(total)"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getWorks } from "@/api/work";
export default {
  name: "WorkMent",
  data() {
    return {
      tableData: [],
      total: 0,
      currentPage: 1, // 当前页数
      pageSize: 10, //每页显示条数
    };
  },
  computed: {
    countData() {
      return this.tableData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      );
    },
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {
      const { data } = await getWorks();
      console.log(data);
      this.tableData = data.data;
      this.total = data.total;
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.currentPage = 1;
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
};
</script>

<style lang="less" scoped>
.el-pagination {
  text-align: left;
}
</style>